package cn.edu.example.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.example.po.Student;
import cn.edu.example.po.User;
import cn.edu.example.service.StudentService;
import cn.edu.example.service.UserService;

@Controller
public class StudentController {
	// ����ע��
	@Autowired
	private StudentService studentservice;
	
	// ����ע��
	@Autowired
	private UserService userService;
	
	/**
	 * �����û���Ϣ
	 */
	@RequestMapping(value = "/student/student_profile.action")
	public String student_profile(HttpSession session,Model model) {
		User user = (User) session.getAttribute("USER_SESSION");
		Student stu = this.studentservice.getStubyID(user.getStu_id());
		model.addAttribute("stu",stu);
	    return "student_profile";
	}
	
	/**
	 * ��ȡ�û���Ϣ
	 */
	@RequestMapping(value = "/student/getStuinfo.action")
	@ResponseBody
	public Student getStuinfo(HttpSession session,Model model) {
		User user = (User) session.getAttribute("USER_SESSION");
		Student stu = this.studentservice.getStuInfo(user.getStu_id());
	    return stu;
	}
	
	/**
	 * �޸��û���Ϣ
	 */
	@RequestMapping(value = "/student/updateStu.action")
	@ResponseBody
	public String updateStu(Student student,HttpSession session,Model model) {
		int rows = this.studentservice.updateStu(student);
		if(rows > 0) {
			// ͨ���˺ź������ѯ�û�
			User user = userService.findUser(student.getUser_name(), student.getPassword());
			// ���û�������ӵ�Session
						session.setAttribute("USER_SESSION", user);
			return "OK";
		}else {
			return "FAIL";
		}
	}
	
	
	@RequestMapping(value = "/student/getAllstudent.action", method = RequestMethod.GET)
	public String getStudentList(Model model) {
		
		List slist=studentservice.getStuList();//浠巗ervices鑾峰彇鏁版嵁
		model.addAttribute("Stu_list",slist);
		return "student_infor";//鎺у埗灞傝烦杞埌jsp
	}
	
	
	
	//绠＄悊鍛樺垹闄や功绫�
	@RequestMapping("/stu/delStu.action")
	@ResponseBody
	public String stuDelete(String stuname) {
		int rows = studentservice.delStu(stuname);
		if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}

	// 绠＄悊鍛橀�氳繃id鑾峰彇鐢ㄦ埛淇℃伅
	@RequestMapping("stu/getStuById.action")
	@ResponseBody
	public Student getStuById(Integer student_id) {	 
		
		System.out.println(student_id);
	   Student student = studentservice.getStuById(student_id);
	   System.out.println(student.getIDcard());
	    return student;
	}
	

	//绠＄悊鍛樻洿鏂拌绋嬩俊鎭�
	@RequestMapping("/student/update.action")
	@ResponseBody
	public String stuUpdate(Student student) {
	    int rows = studentservice.updateStudent(student);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	
	@RequestMapping("stu/getStuByName.action")
	public  String getStuByName(String name,Model model) {
		if(name != "" && name != null) {
			List<Student> students = studentservice.getStubyName(name);
			model.addAttribute("Stu_list",students);
		}else {
			List<Student> students = this.studentservice.getStuList();
			model.addAttribute("Stu_list",students);
		}
	   model.addAttribute("name",name);
	   return "student_infor";
	}
}
